package co2.bean;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.pojos.Material;
import co2.pojos.Silabus;

@ManagedBean
@ViewScoped
public class ListSilabusBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<Silabus> list;
	private Silabus material;
	private String matId;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	@PostConstruct
	public void postCon(){
		list = dao.getAllSilabus();
		Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		map.remove("silabusFromListContent");
	}
	
	public void doDetail(){
		if(material!=null){
			
		}
	}
	public void prosesMat(){

		 if(material!=null){
			  try{
				  	String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
					FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/silabus_master.jsf?faces-redirect=true&idmat="+material.getId());
			  }catch(Exception e){
				  e.printStackTrace();
			  }
		 }
		
	}
	public void detail(){
		Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		map.remove("silabusFront");
		map.put("silabusFromListContent", material);
		try{
			String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
			FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/detail_from_list_silabus.jsf");
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"redirect error",""));
		}
		
	}
	public void delete(){
		if(material!=null){
			try{
				dao.delete(Silabus.class, material.getId());
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Deleted!!",""));
				list = dao.getAllSilabus();
			}catch(Exception e){
				e.printStackTrace();
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Delete Error",""));
			}
		}
	}
	public String getMatId() {
		return matId;
	}

	public void setMatId(String matId) {
		this.matId = matId;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	public List<Silabus> getList() {
		return list;
	}

	public void setList(List<Silabus> list) {
		this.list = list;
	}

	public Silabus getMaterial() {
		return material;
	}

	public void setMaterial(Silabus material) {
		this.material = material;
	}
}